package com.rc.saas.tenant.controller.account;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.controller.msg.TenantNoticeController;
import com.rc.saas.tenant.model.msg.TenantNotice;
import com.rc.saas.tenant.search.account.WithdrawMagSearch;
import com.rc.saas.tenant.search.msg.TenantNoticeSearch;
import com.rc.saas.tenant.service.operator.RcOperatorService;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.account.WithDrawManagerDetailVo;
import com.rc.saas.tenant.vo.account.WithDrawManagerVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author: yang
 * @Date: 2022/7/21 23:22
 */
@Controller
@RequestMapping("/withdrawManager")
public class WithdrawManagerController {
    private static Logger logger = LogManager.getLogger(TenantNoticeController.class);

    @Autowired
    private RcOperatorService rcOperatorService;

    /**
     * 跳转分销商列表(结算中心)
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/templates/account/withdrawManager";
    }


    /**
     * 提成列表
     *
     * @return
     */
    @RequestMapping(value = "/listWithdrawMagVo", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listWithdrawMagVo(WithdrawMagSearch withdrawMagSearch) {
        PageInfo<WithDrawManagerVo> pageInfo = rcOperatorService.listWithdrawManagerVo(withdrawMagSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 提成明细
     *
     * @return
     */
    @RequestMapping(value = "/listWithdrawMagDetailVo", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listWithdrawMagDetailVo(WithdrawMagSearch withdrawMagSearch) {
        PageInfo<WithDrawManagerDetailVo> pageInfo = rcOperatorService.listWithdrawManagerDetailVo(withdrawMagSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 提成明细
     *
     * @return
     */
    @RequestMapping(value = "/detail/index", method = RequestMethod.GET)
    public String listWithdrawMagDetailVo() {
        return "/templates/account/withdrawManagerDetail";
    }

    /**
     * 提成明细
     *
     * @return
     */
    @RequestMapping(value = "/listDetail/{memberId}", method = RequestMethod.GET)
    public String listDetail(Model model, @PathVariable("memberId") Long memberId) {
        model.addAttribute("memberId", memberId);
        return "/templates/account/withdrawManagerDetail";
    }

    /**
     * 导出提成结算列表
     */
    @ResponseBody
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public TResponse export(WithdrawMagSearch withdrawMagSearch, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(withdrawMagSearch);
            PageInfo<WithDrawManagerVo> page = rcOperatorService.listWithdrawManagerVo(withdrawMagSearch);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("提成结算列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), WithDrawManagerVo.class).sheet("提成结算列表").doWrite(page.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出提成结算列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    /**
     * 导出提成明细列表
     */
    @ResponseBody
    @RequestMapping(value = "/exportDetail", method = RequestMethod.GET)
    public TResponse exportDetail(WithdrawMagSearch withdrawMagSearch, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(withdrawMagSearch);
            PageInfo<WithDrawManagerDetailVo> page = rcOperatorService.listWithdrawManagerDetailVo(withdrawMagSearch);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("提成明细列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), WithDrawManagerDetailVo.class).sheet("提成明细列表").doWrite(page.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出提成明细列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    public static void main(String[] args) {
        String img = "";
    }
}
